/**
 * 
 */
package arisufascion.com.activity;

import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.google.gson.JsonObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import arisufascion.com.AbstractActivity;
import arisufascion.com.MainApplication;
import arisufascion.com.R;
import arisufascion.com.adapter.SpinnerAdapter;
import arisufascion.com.constant.Constants;
import arisufascion.com.constant.WriteLog;
import arisufascion.com.db.MySharePreferences;
import arisufascion.com.db.ProductDataSource;
import arisufascion.com.handler.CountryHandler;
import arisufascion.com.handler.ProfileHandler;
import arisufascion.com.handler.ServiceHandler;
import arisufascion.com.handler.StateHandler;
import arisufascion.com.model.Country;
import arisufascion.com.model.Profile;
import arisufascion.com.model.State;
import arisufascion.com.network.RequestTask;
import arisufascion.com.tabs.TabGroupActivity;

/**
 * @author Kelvin
 * 
 */
public class ActivityCheckout extends AbstractActivity {

	MainApplication apps;
	private LinearLayout layout_root;
	private Button btnLogin, btnSignUp, btnCheckOut, btnRegistry;

	private LinearLayout login, sign, checkout;
	private LayoutInflater inflater;

	// define layout for login form
	private EditText edittextUsername;
	private EditText edittextPassword;
	private Button btnSubmit;
	private CheckBox checkBoxForgetPassword;
	private String username, password;
	private boolean isLoading = true;
	private ProgressDialog dialog;
	private Profile profile;
	private ProductDataSource database;
	// define layot for sign form
	// ================================================================
	private EditText sign_username, sign_displayname, sign_password,
			sign_confirm_password, sign_name, sign_email, sign_phone, sign_fax,
			sign_zip, sign_address1, sign_address2, sign_city, sign_first_name,
			sign_last_name, sign_middle_name;

	private String str_username, str_displayname, str_password,
			str_confirm_password, str_title, str_name, str_email, str_mobile,
			str_phone, str_fax, str_zip, str_address1, str_address2, str_city,
			str_state, str_country, str_first_name, str_last_name,
			str_middle_name;

	private List<String> listCountries = new ArrayList<String>();
	private List<Country> counries = new ArrayList<Country>();

	private List<String> listStates = new ArrayList<String>();
	private List<State> states = new ArrayList<State>();

	private int countryId = 0;
	private int stateId = 0;

	private List<String> listCountriesGuest = new ArrayList<String>();
	private List<Country> counries_guest = new ArrayList<Country>();

	private List<String> listStates_guest = new ArrayList<String>();
	private List<State> states_guest = new ArrayList<State>();

	private int countryGuestId = 0;
	private int stateGuestId = 0;
	// ==========================================================================
	private Spinner sign_country, sign_state;
	private Button btn_registry, btn_cancel;
	private TextView btn_for_got_password;

	// define layout for guest form
	// ================================================================
	private EditText guest_username, guest_displayname, guest_password,
			guest_confirm_password, guest_name, guest_email, guest_mobile,
			guest_phone, guest_fax, guest_zip, guest_address1, guest_address2,
			guest_city, guest_first_name, guest_last_name, guest_middle_name;

	private String str_guest_username, str_guest_displayname,
			str_guest_password, str_guest_confirm_password, str_guest_title,
			str_guest_name, str_guest_email, str_guest_mobile, str_guest_phone,
			str_guest_fax, str_guest_zip, str_guest_address1,
			str_guest_address2, str_guest_city, str_guest_state,
			str_guest_country, str_guest_first_name, str_guest_last_name,
			str_guest_middle_name;

	private Spinner guest_country, guest_state;
	private Button guest_btn_checkout, guest_btn_cancel;

	// ====================================================

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View viewToLoad = LayoutInflater.from(getParent()).inflate(
				R.layout.activity_checkout, null);
		this.setContentView(viewToLoad);

		findViewById(R.id.btn_menu).setOnClickListener(onClick);
		findViewById(R.id.btn_privacy).setOnClickListener(onClick);
		findViewById(R.id.btn_wholesale).setOnClickListener(onClick);
		findViewById(R.id.btn_shipping).setOnClickListener(onClick);
		
		// setContentView(R.layout.activity_checkout);
		apps = (MainApplication) getApplication();
		database = new ProductDataSource(getParent());
		database.open();
		initView();

		// set up UI load data for spinner country & state

	}

	public void loadUI() {

	}

	public void initView() {
		inflater = LayoutInflater.from(getParent());
		layout_root = (LinearLayout) findViewById(R.id.layout_root);

		// lookup layout for login form
		login = (LinearLayout) inflater.inflate(R.layout.layout_login, null);

		edittextUsername = (EditText) login.findViewById(R.id.edittextUsername);
		edittextPassword = (EditText) login.findViewById(R.id.edittextPassword);

		btn_for_got_password = (TextView) login
				.findViewById(R.id.btn_for_get_password);

		btn_for_got_password.setOnClickListener(onClickEvent);
		checkBoxForgetPassword = (CheckBox) login
				.findViewById(R.id.checkBoxForgetPassword);
		btnSubmit = (Button) login.findViewById(R.id.btnSubmit);

		// lookup layout for sign form
		sign = (LinearLayout) inflater.inflate(R.layout.layout_sign_up, null);

		btnRegistry = (Button) sign.findViewById(R.id.btn_registry);
		btnRegistry.setOnClickListener(onClickEvent);

		sign_username = (EditText) sign.findViewById(R.id.sign_username);
		sign_displayname = (EditText) sign.findViewById(R.id.sign_displayname);
		sign_password = (EditText) sign.findViewById(R.id.sign_password);
		sign_confirm_password = (EditText) sign
				.findViewById(R.id.sign_confirm_password);
		sign_name = (EditText) sign.findViewById(R.id.sign_name);
		sign_first_name = (EditText) sign.findViewById(R.id.sign_first_name);
		sign_last_name = (EditText) sign.findViewById(R.id.sign_last_name);
		sign_middle_name = (EditText) sign.findViewById(R.id.sign_middle_name);
		sign_email = (EditText) sign.findViewById(R.id.sign_email);
		sign_phone = (EditText) sign.findViewById(R.id.sign_phone);
		sign_fax = (EditText) sign.findViewById(R.id.sign_fax);
		sign_zip = (EditText) sign.findViewById(R.id.sign_zip);
		sign_address1 = (EditText) sign.findViewById(R.id.sign_address1);
		sign_address2 = (EditText) sign.findViewById(R.id.sign_address2);
		sign_city = (EditText) sign.findViewById(R.id.sign_city);
		sign_state = (Spinner) sign.findViewById(R.id.sign_state);
		sign_country = (Spinner) sign.findViewById(R.id.sign_country);
		btn_registry = (Button) sign.findViewById(R.id.btn_registry);
		btn_cancel = (Button) sign.findViewById(R.id.btn_cancel);

		// lookup layout for checkout form
		checkout = (LinearLayout) inflater.inflate(
				R.layout.layout_checkout_guest, null);

		guest_name = (EditText) checkout.findViewById(R.id.guest_name);
		guest_first_name = (EditText) checkout
				.findViewById(R.id.guest_first_name);
		guest_last_name = (EditText) checkout
				.findViewById(R.id.guest_last_name);
		guest_middle_name = (EditText) checkout
				.findViewById(R.id.guest_middle_name);
		guest_email = (EditText) checkout.findViewById(R.id.guest_email);
		guest_mobile = (EditText) checkout.findViewById(R.id.guest_mobile);
		guest_phone = (EditText) checkout.findViewById(R.id.guest_phone);
		guest_fax = (EditText) checkout.findViewById(R.id.guest_fax);
		guest_zip = (EditText) checkout.findViewById(R.id.guest_zip);
		guest_address1 = (EditText) checkout.findViewById(R.id.guest_address1);
		guest_address2 = (EditText) checkout.findViewById(R.id.guest_address2);
		guest_city = (EditText) checkout.findViewById(R.id.guest_city);
		guest_state = (Spinner) checkout.findViewById(R.id.guest_state);
		guest_country = (Spinner) checkout.findViewById(R.id.guest_country);
		guest_btn_checkout = (Button) checkout
				.findViewById(R.id.guest_btn_checkout);
		guest_btn_cancel = (Button) checkout
				.findViewById(R.id.guest_btn_cancel);
		guest_btn_checkout.setOnClickListener(onClickEvent);

		// =========================================

		btnLogin = (Button) findViewById(R.id.btnLogin);
		btnSignUp = (Button) findViewById(R.id.btnSignUp);
		btnCheckOut = (Button) findViewById(R.id.btnCheckOut);

		// set event for all button controller
		findViewById(R.id.btnLogin).setOnClickListener(onClickEvent);
		findViewById(R.id.btnSignUp).setOnClickListener(onClickEvent);
		findViewById(R.id.btnCheckOut).setOnClickListener(onClickEvent);
		// findViewById(R.id.guest_btn_checkout).setOnClickListener(onClickEvent);

		btnSubmit.setOnClickListener(onClickEvent);

		sign_country.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// Toast.makeText(getParent(),
				// parent.getItemAtPosition(position).toString(),
				// Toast.LENGTH_LONG).show();
				countryId = counries.get(arg2).getCountry_id();

				// WriteLog.d(TAG, countryId+"");

				new getState().execute();

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		sign_state.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				if (states != null) {
					stateId = states.get(position).getState_id();
				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		guest_country.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				try {
					countryGuestId = counries_guest.get(arg2).getCountry_id();
				} catch (Exception e) {
					// TODO: handle exception
				}

				new getStateGuest().execute();

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		guest_state.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				try {
					if (states_guest != null) {
						stateGuestId = states_guest.get(position).getState_id();
					}
				} catch (Exception e) {

				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}

	public OnClickListener onClickEvent = new OnClickListener() {

		@SuppressWarnings("deprecation")
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnLogin:
				btnLogin.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.checkout_hover));

				btnSignUp.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.checkout));

				btnCheckOut.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.checkout));

				layout_root.removeAllViews();
				layout_root.addView(login);
				break;

			case R.id.btnSignUp:

				btnLogin.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.checkout));

				btnSignUp.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.checkout_hover));

				btnCheckOut.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.checkout));

				layout_root.removeAllViews();
				layout_root.addView(sign);

				new getCountry().execute();

				break;

			case R.id.btn_for_get_password:
				// process for got password here
				Intent intent_for_got_password = new Intent(getParent(),
						ActivityForGotPassword.class);

				// startActivity(intent_checkout);
				// finish();
				TabGroupActivity tabGroupActivity = (TabGroupActivity) getParent();
				tabGroupActivity.startChildActivity("ActivityForGotPassword",
						intent_for_got_password);
				break;

			case R.id.btnCheckOut:

				btnLogin.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.checkout));

				btnSignUp.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.checkout));

				btnCheckOut.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.checkout_hover));

				layout_root.removeAllViews();
				layout_root.addView(checkout);

				new getCountryCheckoutGuest().execute();

				break;
			case R.id.btnSubmit:
				username = edittextUsername.getText().toString();
				password = edittextPassword.getText().toString();
				if (username.length() < 1) {
					edittextUsername.setError("Username not blank...");
					return;
				}
				if (password.length() < 1) {
					edittextPassword.setError("Password not blank...");
					return;
				}
				// execute checklogin to server
				new checkLogin().execute();

				break;
			case R.id.btn_registry:

				str_username = sign_username.getText().toString();
				str_displayname = sign_displayname.getText().toString();
				str_password = sign_password.getText().toString();
				str_confirm_password = sign_confirm_password.getText()
						.toString();
				str_name = sign_name.getText().toString();
				str_email = sign_email.getText().toString();
				str_phone = sign_phone.getText().toString();
				str_fax = sign_fax.getText().toString();
				str_zip = sign_zip.getText().toString();
				str_address1 = sign_address1.getText().toString();
				str_address2 = sign_address2.getText().toString();
				str_city = sign_city.getText().toString();
				str_first_name = sign_first_name.getText().toString();
				str_last_name = sign_last_name.getText().toString();
				str_middle_name = sign_middle_name.getText().toString();

				// str_country = sign_country.getChildAt(0).toString();

				Spanned error_msg = Html
						.fromHtml("<font color='red'>Invalid Input" + "</font>");

				if (str_username.length() < 1) {
					sign_username.setError(error_msg);
					sign_username.requestFocus();
					return;
				}
				if (str_username.length() < 1) {
					sign_username.setError(error_msg);
					sign_username.requestFocus();
					return;
				}
				if (str_displayname.length() < 1) {
					sign_displayname.setError(error_msg);
					sign_displayname.requestFocus();
					return;
				}
				if (str_password.length() < 1) {
					sign_password.setError(error_msg);
					sign_password.requestFocus();
					return;
				}
				if (str_confirm_password.length() < 1) {
					sign_confirm_password.setError(error_msg);
					sign_confirm_password.requestFocus();
					return;
				} else {
					if (!str_confirm_password.equals(str_password)) {

						Spanned confirmpass = Html
								.fromHtml("<font color='red'>passwords do not match"
										+ "</font>");
						sign_confirm_password.setError(confirmpass);
						sign_confirm_password.requestFocus();
						return;
					}
				}

				if (str_first_name.length() < 1) {
					sign_first_name.setError(error_msg);
					sign_first_name.requestFocus();
					return;
				}

				if (str_last_name.length() < 1) {
					sign_last_name.setError(error_msg);
					sign_last_name.requestFocus();
					return;
				}

				if (str_email.length() > 0) {
					if (!isEmailValid(str_email)) {
						Spanned Mail_is_not_Valid = Html
								.fromHtml("<font color='red'>Mail is not Valid"
										+ "</font>");
						sign_email.setError(Mail_is_not_Valid);
						sign_email.requestFocus();
						return;
					}
				} else {
					Spanned Mail_is_not_Valid = Html
							.fromHtml("<font color='red'>Mail is not Valid"
									+ "</font>");
					sign_email.setError(Mail_is_not_Valid);
					sign_email.requestFocus();
					return;
				}

				if (str_zip.length() < 1) {
					sign_zip.setError(error_msg);
					return;
				}
				if (str_address1.length() < 1) {
					sign_address1.setError(error_msg);
					return;
				}
				// submit data to server for create account
				new createAccount().execute();

				break;
			case R.id.guest_btn_checkout:

				str_guest_name = guest_name.getText().toString();
				str_guest_email = guest_email.getText().toString();
				str_guest_mobile = guest_mobile.getText().toString();
				str_guest_phone = guest_phone.getText().toString();
				str_guest_fax = guest_fax.getText().toString();
				str_guest_zip = guest_zip.getText().toString();
				str_guest_address1 = guest_address1.getText().toString();
				str_guest_address2 = guest_address2.getText().toString();
				str_guest_city = guest_city.getText().toString();
				str_guest_first_name = guest_first_name.getText().toString();
				str_guest_last_name = guest_last_name.getText().toString();
				str_guest_middle_name = guest_middle_name.getText().toString();

				// str_country = sign_country.getChildAt(0).toString();

				Spanned error_msg_guest = Html
						.fromHtml("<font color='red'>Invalid Input" + "</font>");

				if (str_guest_first_name.length() < 1) {
					guest_first_name.setError(error_msg_guest);
					guest_first_name.requestFocus();
					return;
				}

				if (str_guest_last_name.length() < 1) {
					guest_last_name.setError(error_msg_guest);
					guest_last_name.requestFocus();
					return;
				}

				if (str_guest_email.length() > 0) {
					if (!isEmailValid(str_guest_email)) {
						Spanned Mail_is_not_Valid = Html
								.fromHtml("<font color='red'>Mail is not Valid"
										+ "</font>");
						guest_email.setError(Mail_is_not_Valid);
						guest_email.requestFocus();
						return;
					}
				} else {
					Spanned Mail_is_not_Valid = Html
							.fromHtml("<font color='red'>Mail is not Valid"
									+ "</font>");
					guest_email.setError(Mail_is_not_Valid);
					guest_email.requestFocus();
					return;
				}

				if (str_guest_zip.length() < 1) {
					guest_zip.setError(error_msg_guest);
					guest_zip.requestFocus();
					return;
				}
				if (str_guest_address1.length() < 1) {
					guest_address1.setError(error_msg_guest);
					guest_address1.requestFocus();
					return;
				}
				new guestCheckout().execute();

				break;
			default:
				break;
			}

		}
	};

	public class getCountry extends AsyncTask<Void, Void, Boolean> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			// Show Dialog
			dialog = new ProgressDialog(getParent());
			dialog.setMessage("Fetching data ...");
			dialog.show();
		}

		@Override
		protected Boolean doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				String url_country = Constants.SERVER_URL_COUNTRY;

				String json_country = RequestTask.getJsonFromUrl(url_country);
				ServiceHandler service = new ServiceHandler();

				CountryHandler countryHandler = service
						.getCountry(json_country);

				if (countryHandler != null) {
					counries = countryHandler.getData();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			return false;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			// Dismiss Dialog
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}

			if (counries != null) {
				for (int i = 0; i < counries.size(); i++) {
					listCountries.add(counries.get(i).getCountry_name());
				}

				SpinnerAdapter countryAdapter = new SpinnerAdapter(self,
						R.layout.row_spinner, listCountries);

				sign_country.setAdapter(countryAdapter);
			}

		}

	}

	public class getState extends AsyncTask<Void, Void, Boolean> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			// Show Dialog
			dialog = new ProgressDialog(getParent());
			dialog.setMessage("Fetching data ...");
			dialog.show();
		}

		@Override
		protected Boolean doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				String url_state = String.format(Constants.SERVER_URL_STATE,
						countryId, Constants.SERVER_API_KEY);

				String json_state = RequestTask.getJsonFromUrl(url_state);

				WriteLog.d(TAG, json_state);

				ServiceHandler service = new ServiceHandler();

				StateHandler stateHandler = service.getState(json_state);

				if (stateHandler != null) {
					states = stateHandler.getData();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			return false;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			// Dismiss Dialog
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}

			if (states != null) {
				for (int j = 0; j < states.size(); j++) {
					listStates.add(states.get(j).getState_name());
				}

				SpinnerAdapter sign_stateAdapter = new SpinnerAdapter(self,
						R.layout.row_spinner, listStates);

				sign_state.setAdapter(sign_stateAdapter);
			}

		}

	}

	// get country for checkout as guest
	public class getCountryCheckoutGuest extends AsyncTask<Void, Void, Boolean> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			// Show Dialog
			dialog = new ProgressDialog(getParent());
			dialog.setMessage("Fetching data ...");
			dialog.show();
		}

		@Override
		protected Boolean doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				String url_country = Constants.SERVER_URL_COUNTRY;

				String json_country = RequestTask.getJsonFromUrl(url_country);
				ServiceHandler service = new ServiceHandler();

				CountryHandler countryHandler = service
						.getCountry(json_country);
				if (countryHandler != null) {
					counries_guest = countryHandler.getData();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

			return false;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			// Dismiss Dialog
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}

			if(counries_guest!=null){
				for (int i = 0; i < counries_guest.size(); i++) {
					listCountriesGuest.add(counries_guest.get(i).getCountry_name());
				}

				SpinnerAdapter guest_Adapter = new SpinnerAdapter(self,
						R.layout.row_spinner, listCountriesGuest);

				guest_country.setAdapter(guest_Adapter);

			}
		}

	}

	// get state for Guest Checkout
	public class getStateGuest extends AsyncTask<Void, Void, Boolean> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			// Show Dialog
			dialog = new ProgressDialog(getParent());
			dialog.setMessage("Fetching data ...");
			dialog.show();
		}

		@Override
		protected Boolean doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String url_state = String.format(Constants.SERVER_URL_STATE,
					countryGuestId, Constants.SERVER_API_KEY);

			String json_state = RequestTask.getJsonFromUrl(url_state);

			WriteLog.d(TAG, json_state);

			ServiceHandler service = new ServiceHandler();

			StateHandler stateHandler = service.getState(json_state);

			states_guest = stateHandler.getData();

			return false;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			// Dismiss Dialog
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}

			for (int j = 0; j < states_guest.size(); j++) {
				listStates_guest.add(states_guest.get(j).getState_name());
			}

			SpinnerAdapter guest_state_Adapter = new SpinnerAdapter(self,
					R.layout.row_spinner, listStates_guest);

			guest_state.setAdapter(guest_state_Adapter);

		}

	}

	boolean isEmailValid(CharSequence email) {
		return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
	}

	public class checkLogin extends AsyncTask<Void, Void, Boolean> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			// Show Dialog
			dialog = new ProgressDialog(getParent());
			dialog.setMessage("Authenticating...");
			dialog.show();

		}

		@Override
		protected Boolean doInBackground(Void... params) {

			try {
				String json = RequestTask.checkLogin(getParent(), username,
						password);
				ServiceHandler service = new ServiceHandler();
				ProfileHandler profileHandler = service.getProfile(json);
				profile = profileHandler.getData();
				JSONObject jsonObj = new JSONObject(json);
				boolean success = jsonObj.getBoolean("success");
				return success;
			} catch (Exception e) {
				// TODO: handle exception
			}

			return false;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			// Dismiss Dialog
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}
			if (result) {
				apps.setProfile(profile);
				database.insertProfile(profile);

				MySharePreferences.saveLogin(true, getParent());

				Intent intent_checkout = new Intent(getParent(),
						ActivityComfirmCheckOut.class);

				// startActivity(intent_checkout);
				// finish();
				TabGroupActivity tabGroupActivity = (TabGroupActivity) getParent();
				tabGroupActivity.startChildActivity("ActivityComfirmCheckOut",
						intent_checkout);

			}

		}

	}

	public class forGotPassword extends AsyncTask<Void, Void, Boolean> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// Show Dialog
			dialog = new ProgressDialog(getParent());
			dialog.setMessage("Please for wait...");
			dialog.setCancelable(true);
			dialog.show();

		}

		@Override
		protected Boolean doInBackground(Void... params) {

			return false;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			// Dismiss Dialog
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}
			if (result) {
				// show message success for got password here

			}
		}

	}

	public class createAccount extends AsyncTask<Void, Void, Boolean> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			// Show Dialog
			dialog = new ProgressDialog(getParent());
			dialog.setMessage("Please for wait...");
			dialog.setCancelable(true);
			dialog.show();

		}

		@Override
		protected Boolean doInBackground(Void... params) {

			try {
				Profile p = new Profile();
				p.setUsername(str_username);
				p.setName(str_displayname);
				p.setCompany(str_name);
				p.setPassword(str_password);
				p.setTitle(str_title);
				p.setEmail(str_email);
				p.setFirst_name(str_first_name);
				p.setMiddle_name(str_middle_name);
				p.setLast_name(str_last_name);
				p.setAddress_1(str_address1);
				p.setAddress_2(str_address2);
				p.setZip(str_zip);
				p.setCity(str_city);
				p.setCountry_id(str_country);
				p.setState_id(str_state);
				p.setPhone_1(str_phone);
				p.setPhone_2(str_mobile);
				p.setFax(str_fax);
				p.setState_id(String.valueOf(stateId));
				p.setCountry_id(String.valueOf(countryId));

				String json = RequestTask.creatAccount(getParent(), p);

				ServiceHandler service = new ServiceHandler();
				ProfileHandler profileHandler = service.getProfile(json);
				profile = profileHandler.getData();
				JSONObject jsonObj = new JSONObject(json);
				boolean success = jsonObj.getBoolean("success");
				return success;

			} catch (Exception e) {
				// TODO: handle exception
			}

			return false;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			if (result) {

				database.deletedAllProfile();

				database.insertProfile(profile);

				apps.setProfile(profile);

				Intent intent_comfirm_checkout = new Intent(getParent(),
						ActivityComfirmCheckOut.class);
				// startActivity(intent_comfirm_checkout);

				TabGroupActivity tabGroupActivity = (TabGroupActivity) getParent();
				tabGroupActivity.startChildActivity("ActivityComfirmCheckOut",
						intent_comfirm_checkout);
			}

			// Dismiss Dialog
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}

		}
	}

	// ===============Guest Checkout ====================

	public class guestCheckout extends AsyncTask<Void, Void, Boolean> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			// Show Dialog
			dialog = new ProgressDialog(getParent());
			dialog.setMessage("Please for wait...");
			dialog.setCancelable(true);
			dialog.show();

		}

		@Override
		protected Boolean doInBackground(Void... params) {

			try {
				Profile profileGuest = new Profile();
				profileGuest.setEmail(str_guest_email);
				profileGuest.setFirst_name(str_guest_first_name);
				profileGuest.setMiddle_name(str_guest_middle_name);
				profileGuest.setLast_name(str_guest_last_name);
				profileGuest.setAddress_1(str_guest_address1);
				profileGuest.setAddress_2(str_guest_address2);
				profileGuest.setZip(str_guest_zip);
				profileGuest.setCity(str_guest_city);
				profileGuest.setCountry_id(str_guest_country);
				profileGuest.setState_id(str_guest_state);
				profileGuest.setPhone_1(str_guest_phone);
				profileGuest.setPhone_2(str_guest_mobile);
				profileGuest.setFax(str_guest_fax);
				profileGuest.setState_id(String.valueOf(stateGuestId));
				profileGuest.setCountry_id(String.valueOf(countryGuestId));

				database.insertProfile(profileGuest);
				apps.setProfileGuest(profileGuest);

				return true;

			} catch (Exception e) {
				// TODO: handle exception
			}

			return false;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			if (result) {

				Intent intent_comfirm_checkout = new Intent(getParent(),
						ActivityComfirmCheckOut.class);
				// startActivity(intent_comfirm_checkout);

				TabGroupActivity tabGroupActivity = (TabGroupActivity) getParent();
				tabGroupActivity.startChildActivity("ActivityComfirmCheckOut",
						intent_comfirm_checkout);
			}

			// Dismiss Dialog
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}

		}
	}

	// -====================End Guest check out==============
	protected void onResume() {
		super.onResume();
		btnLogin.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.checkout_hover));

		btnSignUp.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.checkout));

		btnCheckOut.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.checkout));

		layout_root.removeAllViews();
		layout_root.addView(login);

	};
}
